<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- table栏切换 -->
          <el-tab-pane label="登录账户设置">
            <!-- 放置表单 -->
            <el-form ref="userForm" :model="userInfo" :rules="rules" label-width="120px" style="margin-left: 120px; margin-top:30px">
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="userInfo.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="密码:" prop="password2">
                <el-input v-model="userInfo.password2" style="width:300px" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="saveUser">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <!-- 打印 -->
            <el-row type="flex" justify="end">
              <el-tooltip content="打印个人基本信息">
                <router-link :to="`/employees/print/${userId}?type=personal`">
                  <i class="el-icon-printer" />
                </router-link>
              </el-tooltip>
            </el-row>
            <!--
              放置内容
              vue.js中 内置了一个组件 component 可以是任何组件
              动态组件 - 可以切换组件
              通过改变 is 的值，切换组件
              最终显示的组件，是根据 is 确定的
              最终的DOM节点是取决小于is
              动态组件可以使用v-if替换
            -->
            <component :is="UserComponent" />
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <!-- 和上面的动态组件一样 -->
            <el-row type="flex" justify="end">
              <el-tooltip content="打印岗位信息">
                <router-link :to="`/employees/print/${userId}?type=job`">
                  <i class="el-icon-printer" />
                </router-link>
              </el-tooltip>
            </el-row>
            <JobInfo />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
// 保存员工信息
import { saveUserDetailById } from '@/api/employees'
// 获取员工信息
import { getFhotoTD } from '@/api/user'
import UserInfo from './components/user-info.vue'
import JobInfo from './components/job-info.vue'
export default {
  components: {
    UserInfo,
    JobInfo
  },
  data() {
    return {
      // 直接将路由中的参数赋值给data中的数据
      userId: this.$route.params.id,
      userInfo: {
        username: '',
        password2: ''
      },
      rules: {
        username: [{ required: true, message: '用户名不能为空', trigger: 'blur' }],
        password2: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 9, message: '密码长度在 6~9 之间', trigger: 'blur' }
        ]
      },
      UserComponent: 'UserInfo'
    }
  },
  created() {
    this.getFhotoTD()
  },
  methods: {
    // 获取员工信息
    async getFhotoTD() {
      this.userInfo = await getFhotoTD(this.userId)
    },
    // 保存员工共信息
    // async saveUser() {
    //   try {
    //     await this.$refs.userForm.validate()
    //     // 使用新密码替换旧密码
    //     await saveUserDetailById({ ...this.userInfo, password: this.userInfo.password2 })
    //     this.$message.success('保存成功')
    //   } catch (error) {
    //     console.log(error)
    //   }
    // }
    saveUser() {
      this.$refs.userForm.validate(async isOk => {
        if (isOk) {
          await saveUserDetailById({ ...this.userInfo, password: this.userInfo.password2 })
          this.$message.success('修改账户信息成功')
        }
      })
    }
  }
}
</script>

<style>

</style>
